<!--
 * @Description: 数据字典管理
 * @Author: wangwangwang
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-08-05 16:47:59
-->
<template>
  <div class="pages_content dictionary-container">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <el-form ref="form" :inline="true" :model="form">
                <el-form-item label="字典名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入" @keyup.enter.native="initData()"></el-input>
                </el-form-item>
                <el-form-item label="所属系统" prop="systemId">
                  <el-select v-model="form.systemId" clearable placeholder="请选择">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="编码" prop="code">
                  <el-input v-model="form.code" placeholder="请输入" @keyup.enter.native="initData()"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="initData()">查 询</el-button>
                  <el-button size="medium" class="bottom_all_two" @click="resetData()">重 置</el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" icon="el-icon-plus" size="small" class="bottom_all" style="margin-left: 0;" @click="add()">添加</el-button>
              <!-- <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" icon="el-icon-check" size="small" class="bottom_three" @click="updateType(1)">启用</el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="updateType(2)">
                <i class="el-icon-remove-outline el-icon--left" style="transform: rotate(-45deg);"></i>
                禁用
              </el-button> -->
              <!-- <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='SHANCHU'))!==-1" icon="el-icon-close" size="small" class="bottom_two" @click="updateType(3)">删除</el-button> -->
            </div>
            <div>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='BAOCUNPXH'))!==-1" :underline="false" @click="updateSortNum()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_sort1"></i>
                </span>
                保存排序号
              </el-link>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="$Download(DownloadURL, form)">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark">
              <template v-for="(item, index) in table">
                <el-table-column v-if="item.status" :key="index" :label="item.label" align="center" :width="item.width" :fixed=" item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <div v-if="item.label == '状态'">
                      <span v-if="scope.row.status == 0" style="color:#969799;">禁用</span>
                      <span v-if="scope.row.status == 1" style="color:#00A754;">启用</span>
                    </div>
                    <el-button v-else-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="update(scope.row)"><i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>修改</el-button>
                    <div v-else-if="item.label == '所属系统'">{{ options |fromatTable(scope.row[item.prop],'name','id')}}</div>
                    <div v-else-if="item.label == '排序号'">
                      <el-input v-model="scope.row.sortNum"></el-input>
                    </div>
                    <span v-else>{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :page-size="pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" :current-page.sync="page" @size-change="sizeChange" @current-change="initData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" class="bottom_all_two" @click="initData()">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="980px" :close-on-click-modal="false" :before-close="resefrom">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>
      <el-form ref="refdialogForm" :inline="true" :rules="rules" :model="dialogForm" label-width="80px">
        <el-form-item prop="code">
          <span slot="label" style="color:#FF0000;">编码</span>
          <el-input v-model="dialogForm.code" style="width: 240px;" placeholder="请输入" :disabled="dialogForm.id!=undefined"></el-input>
        </el-form-item>
        <el-form-item prop="name">
          <span slot="label" style="color:#FF0000;">名称</span>
          <el-input v-model="dialogForm.name" style="width: 240px;" placeholder="请输入" :disabled="dialogForm.id!=undefined"></el-input>
        </el-form-item>
        <el-form-item label="所属系统" prop="systemId">
          <!-- <el-input v-model="dialogForm.systemId" placeholder="请输入"></el-input> -->
          <el-select v-model="dialogForm.systemId" placeholder="请选择" style="width:240px" :disabled="dialogForm.id!=undefined">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <br />
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dialogForm.remark" style="width: 600px;" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <div style="padding-bottom: 10px;">
        <el-button size="mini" class="bottom_all" style="margin-left: 0;" @click="addItemData()">
          <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
          添加
        </el-button>
      </div>
      <el-table :data="dialogTable" style="width: 100%">
        <el-table-column align="center" label="成员名称" width="220">
          <template slot-scope="scope">
            <el-input v-model="scope.row.itemName" :disabled="!!scope.row.id"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="成员值" width="120">
          <template slot-scope="scope">
            <el-input v-model="scope.row.itemValue" :disabled="!!scope.row.id"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="成员类别">
          <template slot-scope="scope">
            <el-input v-model="scope.row.itemType"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="排序号">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sortNum"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enable===1" type="success">启用</el-tag>
            <el-tag v-if="scope.row.enable===0" type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="120">
          <template slot-scope="scope">
            <el-button size="mini" class="bottom_one" @click="handleItem(scope.row)">
              {{scope.row.enable===1?'禁用':'启用'}}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom">取 消</el-button>
        <el-button :loading="submitLoading" class="bottom_all" @click="sureSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDictionaryData,
  editDictionary,
  getDetail,updateSortnum,
  updateOrDelete, Download, handleChild,
} from "@/api/basedata/dictionary";
import breaDcrumb from "@/components/breadcrumb";
import { mapGetters } from "vuex";
import { getSystemlist } from "@/api/basedata/business.js";
import { setTimeout } from 'timers';
export default {
  components: {
    breaDcrumb,
  },
  data() {
    return {
      DownloadURL: Download,
      /*添加修改弹窗*/
      dialogVisible: false,
      submitLoading: false,
      dialogtitle: "添加数据字典",
      /*弹要修改过着添加的数据*/
      from: "",
      fromId: "",
      /*判断收起展开*/
      showCardNav: true,
      /*搜索关键字段*/
      keyword: "",
      code: "",
      /*列表展示*/
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
      },
      table: [
        { label: "编码", width: "auto", prop: "code", status: true },
        { label: "名称", width: "auto", prop: "name", status: true },
        { label: "所属系统", width: "auto", prop: "systemId", status: true },
        { label: "状态", width: "auto", prop: "status", status: true },
        { label: "排序号", width: "auto", prop: "sortNum", status: true },
        { label: "备注", width: "auto", prop: "remark", status: true },
        { label: "操作", width: "auto", prop: "", status: true },
      ],
      /*列表选中的数据集*/
      options: [],
      SelectionTableData: [],
      tableData: [],
      total: 0,
      dialogTable: [],
      olddialogTable: [],
      value: "",
      textarea: "",
      page: 1,
      pageSize: 10,
      dialogForm: {
        code: "",
        name: "",
        systemId: "",
        remark: "",
      },
      form: {
        code: "",
        name: "",
      },
      addindex: 0,
      ButtonList: [],
    };
  },
  methods: {
    resefrom() {
      this.dialogVisible = false;
      this.$refs.refdialogForm.resetFields();
    },
    delItemData(index) {
      this.dialogTable.splice(index, 1);
    },
    handleItem(data) {
      const params = {
        ids: [data.id],
        status: !data.enable ? 1 : 0,
        type: 'update_enable',
      }
      handleChild(params).then(res => {
        if (res.code === 1001) {
          this.$message.success('操作成功');
          data.enable = !data.enable ? 1 : 0;
        }
      })
      console.log(data)
    },
    updateSortNum() {
      const sorts = []
      this.tableData.forEach((v) => {
        sorts.push({ id: v.id, sortNum: v.sortNum })
      })
      updateSortnum(sorts).then((res) => {
        if (res.code === 1001) {
          this.$message.success('更新成功')
          this.initData();
        }
      });
    },
    initData() {
      let params = {
        ...this.form,
        page: this.page,
        pageSize: this.pageSize,
      };
      getDictionaryData(params).then((res) => {
        this.tableData = res.data.data;
        this.total = res.data.totalRows;
      });
    },
    resetData() {
      this.$refs.form.resetFields();
      this.initData();
    },
    add() {
      let _this = this;
      _this.dialogVisible = true;
      this.$data.dialogForm = this.$options.data().dialogForm;
      this.dialogTable = this.$options.data().dialogTable;
      this.olddialogTable = [];
      setTimeout(() => {
        this.$refs.dialogForm.clearValidate();
      }, 1000);
      _this.dialogtitle = "添加数据字典";
      _this.addindex = 0;
    },
    updateType(index) {
      const self = this;
      this.$confirm(
        `确定${index === 1 ? "启用" : index === 2 ? "禁用" : "删除"
        }选中的数据?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          cancelButtonClass: 'bottom_all_two',
          confirmButtonClass: 'bottom_all',
          type: "warning",
        }
      )
        .then(() => {
          switch (index) {
            case 1: {
              let params = {
                status: 1,
                type: "update_enable",
              };
              self.handleData(params);
              return;
            }
            case 2: {
              let params = {
                status: 0,
                type: "update_enable",
              };
              self.handleData(params);
              return;
            }
            case 3: {
              let params = {
                status: 0,
                type: "update_status",
              };
              self.handleData(params);
              return;
            }
            default:
              break;
          }
        })
        .catch(() => { });
    },
    handleData(data) {
      const params = {
        ...data,
        ids: this.SelectionTableData,
      };
      updateOrDelete(params).then((res) => {
        console.log(res);
        this.initData();
        this.$message({
          message: "操作成功！",
          type: "success",
        });
      });
    },
    update(data) {
      /**
       * @Description: 修改数据字典
       * @Param:
       * @Author: wangwangwang
       */
      this.dialogVisible = true;
      getDetail({ id: data.id }).then((res) => {
        this.dialogtitle = "修改数据字典";
        this.dialogTable = [];
        this.olddialogTable = []
        if (res.data.items) {
          res.data.items.forEach((item) => {
            let j = {
              itemName: item.itemName,
              itemValue: item.itemValue,
              itemType: item.itemType,
              sortNum: item.sortNum,
              status: item.status,
              remark: item.remark,
              id: item.id,
              enable: item.enable,
            };
            this.dialogTable.push(j);
            this.olddialogTable.push(j);
          });
        }
        const oject = {
          code: res.data.code,
          name: res.data.name,
          systemId: res.data.systemId,
          remark: res.data.remark,
          id: res.data.id,
        };
        this.dialogForm = oject;
      });
    },
    addItemData() {
      const data = {
        itemName: "",
        itemValue: "",
        itemType: "",
        enable: 1,
        sortNum: this.addindex + 1,
        remark: "",
        addindex: this.addindex,
      };
      this.dialogTable.push(data);
      this.addindex = this.addindex + 1;
    },
    sureSubmit() {
      this.$refs.refdialogForm.validate((valid) => {
        if (valid) {
          let allHasData = this.dialogTable.filter((item) =>
            this.olddialogTable.some((items) => item.id == items.id)
          );
          let deleteData = this.olddialogTable.filter(
            (item) => !allHasData.some((items) => item.id == items.id)
          );
          let addData = this.dialogTable.filter(
            (item) => !allHasData.some((items) => item.id == items.id)
          );
          deleteData.forEach((res) => {
            res.status = 0;
          });
          const params = {
            ...this.dialogForm,
            items: [...allHasData, ...deleteData, ...addData],
          };
          this.submitLoading = true;
          editDictionary(params).then((res) => {
            if (res.code === 1001) {
              this.$message({
                message: "操作成功！",
                type: "success",
              });
              this.initData();
            }
            this.submitLoading = false;
            this.resefrom();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sizeChange(value) {
      this.pageSize = value;
      this.initData();
    },
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
    getSystemlist().then((res) => {
      this.options = res.data;
    });
    this.initData();
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 124;
      } else {
        num = this.screenWidth - 220;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style lang="scss" scoped>
.dictionary-container {
  .el-link {
    i {
      font-size: 14px;
      color: $bg_color;
    }
  }
  .el-link:hover {
    color: $bg_color;
  }
  .header_right {
    @include discen_al;
    padding: 0 20px;
    justify-content: space-between;
    .title {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: bold;
    }
  }
  font-size: $font_size;
  .el-main {
    padding: 0;
  }
  .el-main::-webkit-scrollbar {
    width: 3px;
    height: 3px;
  }
  .el-main::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
    border-radius: 2px;
  }
  .el-main::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }
  .el-main::-webkit-scrollbar-thumb:hover {
    background: #333;
  }
  .el-main::-webkit-scrollbar-corner {
    background: #179a16;
  }
  .search {
    padding: 20px;
  }
  .card_nav {
    padding: 0 20px;
    height: 76px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .card_info {
    padding: 0 20px;
  }
  .card_bottom {
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 72px;
    .card_bottom_left {
      display: flex;
      align-items: center;
      font-size: $font_size;
    }
  }
}
</style>
